package cn.edu.lingnan.lsqn.service;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import cn.cdahua.basic.model.Pager;
import cn.edu.lingnan.lsqn.dao.IArticleDao;
import cn.edu.lingnan.lsqn.model.Article;
import cn.edu.lingnan.lsqn.model.ArticleType;

@Service("articleService")
public class ArticleService implements IArticleService {

	private IArticleDao articleDao;

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	@Inject
	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	@Override
	public void add(Article article) {
		article.setCreateDate(new Date());
		article.setStatus(ArticleType.UNREAD);
		articleDao.add(article);
	}

	@Override
	public void delete(int id) {
		articleDao.delete(id);
	}

	@Override
	public void update(Article article) {
		Article oldArticle = articleDao.load(article.getId());
		oldArticle.setTitle(article.getTitle());
		oldArticle.setContent(article.getContent());
		articleDao.update(oldArticle);
	}

	@Override
	public void updateStatus(int id, ArticleType status) {
		Article oldArticle = articleDao.load(id);
		if (oldArticle.getStatus().equals(status))
			return;
		oldArticle.setStatus(status);
		oldArticle.setReadDate(new Date());
		articleDao.update(oldArticle);
	}

	@Override
	public Pager<Article> find() {
		return articleDao.find();
	}

	@Override
	public Pager<Article> findByUserId(int uid) {
		return articleDao.findByUserId(uid);
	}

	@Override
	public Article load(int id) {
		return articleDao.load(id);
	}

	@Override
	public Pager<Article> findByStatus(ArticleType status) {
		return articleDao.findByStatus(status);
	}

	@Override
	public Pager<Article> findByUserIdAndStatus(int uid,ArticleType status) {
		return articleDao.findByUserIdAndStatus(uid,status);
	}

}
